


let obj = {
    a: 1,
    b: function () {
        console.log('obj.b')
    },
    c: {
        d: 'c.d'
    }
}

const type = (target) => {
    return Object.prototype.toString.call(target).slice(8, -1)
}

const deepClone = (target) => {
    let result = {}
    for (let key in target) {
        let value = target[key]
        if (['String', 'Number', 'undefined', 'Null'].includes(type(value))) { // 基本类型
            result[key] = value
        } else if (type(value) === 'Object') {
            result[key] = deepClone(value)
        } else if (type(value) === 'Function') {
            result[key] = Object.create(value)
        }
    }

    return result
}

// let copyObj = Object.create(obj)
let copyObj = deepClone(obj)
copyObj.a = 2
copyObj.b = function () {
    console.log('b++.b')
}
copyObj.c.d = 'c++.d'
console.log(obj)
// { a: 1, b: [Function: b], c: { d: 'c++.d' } } 基础类型无影响，引用类型影响
// { a: 1, b: [Function: b], c: { d: 'c.d' } } 影响